package com.gohome.gohomechat.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.gohome.gohomechat.GoHomeModel;
import com.gohome.gohomechat.model.entify.Contant;
import com.gohome.gohomechat.model.entify.CurrentUser;
import com.gohome.gohomechat.model.entify.Friend;
import com.gohome.gohomechat.model.entify.Notice;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/3/3.
 */
public class GoHomeDBTools {
    //  private NoticeSQLiteHelper helper;
    private SQLiteDatabase db;
    private Context context;
    private DBOpenHelper dbOpenHelper;

    private static GoHomeDBTools dbTools;

    private GoHomeDBTools(Context context) {
        this.context = context;
        // this.helper = new NoticeSQLiteHelper(context, Contant.DB_NOTICE, null, Contant.VERSION);
        // this.db = helper.getWritableDatabase();
        this.dbOpenHelper = new DBOpenHelper(context, Contant.DB_NOTICE + GoHomeModel.getInstance().getCurrentUserName(), null, Contant.VERSION);
        this.db = dbOpenHelper.getWritableDatabase();
    }

    public static void init(Context context) {
        if (dbTools == null)
            dbTools = new GoHomeDBTools(context);
    }

    public static GoHomeDBTools getInstance() {
        return dbTools;
    }

    /**
     * 添加多个数据
     */
    public void saveDatasToDataBase(List<Notice> datas) {
        ContentValues values = new ContentValues();
        for (Notice data : datas) {
            String head = data.getHead();
            String userName = data.getUserName();
            String reason = data.getReason();
            long time = data.getTime();
            int messageStatus = data.getMessageStatus();
            String groupId = data.getGroupId();
            String groupName = data.getGroupName();

            values.put("head", head);
            values.put("username", userName);
            values.put("reason", reason);
            values.put("time", time);
            values.put("messagestatus", messageStatus);
            values.put("groupid", groupId);
            values.put("groupname", groupName);
            db.insert(Contant.TABLE_NOTICE, null, values);
            values.clear();
        }
    }

    /**
     * 插入一条数据
     */
    public void insert(Notice notice) {
        ContentValues values = new ContentValues();
        String head = notice.getHead();
        String userName = notice.getUserName();
        String reason = notice.getReason();
        long time = notice.getTime();
        int messageStatus = notice.getMessageStatus();
        String groupId = notice.getGroupId();
        String groupName = notice.getGroupName();

        values.put("head", head);
        values.put("username", userName);
        values.put("reason", reason);
        values.put("time", time);
        values.put("messagestatus", messageStatus);
        values.put("groupid", groupId);
        values.put("groupname", groupName);
        db.insert(Contant.TABLE_NOTICE, null, values);
    }

    /**
     * 查询所有数据
     */
    public List<Notice> queryAllDatas() {
        Cursor cursor = db.query(Contant.TABLE_NOTICE, null, null, null, null, null, null);
        List<Notice> notices = new ArrayList<>();
        if (cursor != null) {
            while (cursor.moveToNext()) {
                String head = cursor.getString(cursor.getColumnIndex("head"));
                String userName = cursor.getString(cursor.getColumnIndex("username"));
                String reason = cursor.getString(cursor.getColumnIndex("reason"));
                long time = cursor.getLong(cursor.getColumnIndex("time"));
                int messageStatus = cursor.getInt(cursor.getColumnIndex("messagestatus"));
                String groupid = cursor.getString(cursor.getColumnIndex("groupid"));
                String groupname = cursor.getString(cursor.getColumnIndex("groupname"));

                Notice notice = new Notice();
                notice.setHead(head);
                notice.setUserName(userName);
                notice.setReason(reason);
                notice.setTime(time);
                notice.setMessageStatus(messageStatus);
                notice.setGroupId(groupid);
                notice.setGroupName(groupname);
                notices.add(0, notice);
            }
            cursor.close();
            return notices;
        }
        return null;
    }

    /**
     * 删除符合条件数据
     */
    public void deleteName(String userName) {
        db.delete(Contant.TABLE_NOTICE, "username = ?", new String[]{userName});
    }

    /**
     * 更新符合条件数据
     */
    public void update(String username, Notice notice) {
        ContentValues values = new ContentValues();
        String head = notice.getHead();
        String userName = notice.getUserName();
        String reason = notice.getReason();
        long time = notice.getTime();
        int messageStatus = notice.getMessageStatus();
        String groupId = notice.getGroupId();
        String groupName = notice.getGroupName();

        values.put("head", head);
        values.put("username", userName);
        values.put("reason", reason);
        values.put("time", time);
        values.put("messagestatus", messageStatus);
        values.put("groupid", groupId);
        values.put("groupname", groupName);
        db.update(Contant.TABLE_NOTICE, values, "username = ?", new String[]{username});
    }

    /**
     * 更新消息状态
     */
    public void updateMessageStatus(String username, int messageStatus) {

        String sql = "select * from " + Contant.TABLE_NOTICE + " where username = ? ";
        Cursor cursor = db.rawQuery(sql, new String[]{username});
        if (cursor != null) {
            while (cursor.moveToNext()) {
                String head = cursor.getString(cursor.getColumnIndex("head"));
                String userName = cursor.getString(cursor.getColumnIndex("username"));
                String reason = cursor.getString(cursor.getColumnIndex("reason"));
                long time = cursor.getLong(cursor.getColumnIndex("time"));
                String groupId = cursor.getString(cursor.getColumnIndex("groupid"));
                String groupName = cursor.getString(cursor.getColumnIndex("groupname"));

                ContentValues values = new ContentValues();
                values.put("head", head);
                values.put("username", userName);
                values.put("reason", reason);
                values.put("time", time);
                values.put("messagestatus", messageStatus);
                values.put("groupid", groupId);
                values.put("groupname", groupName);
                db.update(Contant.TABLE_NOTICE, values, "username = ?", new String[]{username});
            }
            cursor.close();
        }
    }

    /**
     * 查询符合条件数据
     */
    public List<Notice> queryName(String username) {
        String sql = "select * from " + Contant.TABLE_NOTICE + " where username = ? ";
        Cursor cursor = db.rawQuery(sql, new String[]{username});
        List<Notice> notices = new ArrayList<>();
        if (cursor != null) {
            while (cursor.moveToNext()) {
                String head = cursor.getString(cursor.getColumnIndex("head"));
                String userName = cursor.getString(cursor.getColumnIndex("username"));
                String reason = cursor.getString(cursor.getColumnIndex("reason"));
                long time = cursor.getLong(cursor.getColumnIndex("time"));
                int messageStatus = cursor.getInt(cursor.getColumnIndex("messagestatus"));
                String groupid = cursor.getString(cursor.getColumnIndex("groupid"));
                String groupname = cursor.getString(cursor.getColumnIndex("groupname"));

                Notice notice = new Notice();
                notice.setHead(head);
                notice.setUserName(userName);
                notice.setReason(reason);
                notice.setTime(time);
                notice.setMessageStatus(messageStatus);
                notice.setGroupId(groupid);
                notice.setGroupName(groupname);
                notices.add(notice);
            }
            cursor.close();
            return notices;
        }
        return null;
    }

    /**
     * 添加多个数据
     */
    public void saveDatasToDataBase_friend(List<Friend> datas) {
        ContentValues values = new ContentValues();
        for (Friend data : datas) {
            String head = data.getHead();
            String userName = data.getUserName();
            String pinyin = data.getPinyin();

            values.put("head", head);
            values.put("username", userName);
            values.put("pinyin", pinyin);
            db.insert(Contant.TABLE_FRIEND, null, values);
            values.clear();
        }
    }

    /**
     * 插入一条数据
     */
    public void insert_friend(Friend friend) {
        ContentValues values = new ContentValues();
        String head = friend.getHead();
        String userName = friend.getUserName();
        String pinyin = friend.getPinyin();

        values.put("head", head);
        values.put("username", userName);
        values.put("pinyin", pinyin);
        db.insert(Contant.TABLE_FRIEND, null, values);
    }

    /**
     * 查询所有数据
     */
    public List<Friend> queryAllDatas_friend() {
        Cursor cursor = db.query(Contant.TABLE_FRIEND, null, null, null, null, null, null);
        List<Friend> friends = new ArrayList<>();
        if (cursor != null) {
            while (cursor.moveToNext()) {
                String head = cursor.getString(cursor.getColumnIndex("head"));
                String userName = cursor.getString(cursor.getColumnIndex("username"));
                String pinyin = cursor.getString(cursor.getColumnIndex("pinyin"));

                Friend friend = new Friend();
                friend.setHead(head);
                friend.setUserName(userName);
                friend.setPinyin(pinyin);
                friends.add(friend);
            }
            cursor.close();
            return friends;

        }
        return null;
    }

    /**
     * 查询符合条件数据
     */
    public int queryName_friend(String username) {
        String sql = "select * from " + Contant.TABLE_NOTICE + " where username = ? ";
        Cursor cursor = db.rawQuery(sql, new String[]{username});
        return cursor.getCount();
    }

    /**
     * 删除符合条件数据
     */
    public void deleteName_friend(String userName) {
        db.delete(Contant.TABLE_FRIEND, "username = ?", new String[]{userName});
    }

    /**
     * 删除全部数据
     */
    public void deleteAllDatas_friend() {
        db.delete(Contant.TABLE_FRIEND, null, null);
    }


    /**
     * 插入一条数据
     */
    public void insert_personal(CurrentUser user) {
        ContentValues values = new ContentValues();
        String head = user.getHead();
        String singnature = user.getSingnature();
        String birthday = user.getBirthday();
        String sex = user.getSex();
        String age = user.getAge();
        String zodiaz = user.getZodiaz();
        String boold = user.getBoold();
        String email = user.getEmail();
        String nick = user.getNick();


        values.put("head", head);
        values.put("username", GoHomeModel.getInstance().getCurrentUserName());
        values.put("nick", nick);
        values.put("singnature", singnature);
        values.put("birthday", birthday);
        values.put("sex", sex);
        values.put("age", age);
        values.put("zodiaz", zodiaz);
        values.put("boold", boold);
        values.put("email", email);
        db.insert(Contant.TABLE_PERSONAL, null, values);
    }

    /**
     * 查询所有数据
     */
    public List<CurrentUser> queryAllDatas_personal() {
        Cursor cursor = db.query(Contant.TABLE_PERSONAL, null, null, null, null, null, null);
        List<CurrentUser> currentUsers = new ArrayList<>();
        if (cursor != null) {
            while (cursor.moveToNext()) {
                String head = cursor.getString(cursor.getColumnIndex("head"));
                String username = cursor.getString(cursor.getColumnIndex("username"));
                String nick = cursor.getString(cursor.getColumnIndex("nick"));
                String singnature = cursor.getString(cursor.getColumnIndex("singnature"));
                String birthday = cursor.getString(cursor.getColumnIndex("birthday"));
                String sex = cursor.getString(cursor.getColumnIndex("sex"));
                String age = cursor.getString(cursor.getColumnIndex("age"));
                String zodiaz = cursor.getString(cursor.getColumnIndex("zodiaz"));
                String boold = cursor.getString(cursor.getColumnIndex("boold"));
                String email = cursor.getString(cursor.getColumnIndex("email"));

              CurrentUser user=new CurrentUser();
                user.setHead(head);
                user.setNick(nick);
                user.setSingnature(singnature);
                user.setBirthday(birthday);
                user.setSex(sex);
                user.setAge(age);
                user.setZodiaz(zodiaz);
                user.setBoold(boold);
                user.setEmail(email);
                currentUsers.add(user);
            }
            cursor.close();
            return currentUsers;
        }
        return null;
    }

    /**
     * 删除全部数据
     */
    public void deleteAllDatas_personal() {
        db.delete(Contant.TABLE_PERSONAL, null, null);
    }
}
